﻿namespace Devonline.Entity;

/// <summary>
/// 销售订单
/// 数据规模: 万
/// </summary>
[Table("sale_order"), DisplayName("销售订单")]
public class SaleOrder : SaleOrder<string>, IEntitySet, IEntitySetWithCreate, IEntitySetWithCreateAndUpdate
{
    /// <summary>
    /// 客户
    /// </summary>
    public virtual Customer? Customer { get; set; }
    /// <summary>
    /// 销售订单明细
    /// </summary>
    public virtual ICollection<SaleOrderDetail>? Details { get; set; }
    /// <summary>
    /// 销售订单货运单记录明细
    /// </summary>
    public virtual ICollection<SaleOrderTransport>? Transports { get; set; }
    /// <summary>
    /// 销售订单支付记录
    /// </summary>
    public virtual ICollection<SaleOrderPaymentRecord>? PaymentRecords { get; set; }
}

/// <summary>
/// 销售订单
/// 数据规模: 万
/// </summary>
[Table("sale_order"), DisplayName("销售订单")]
public abstract class SaleOrder<TKey> : Order<TKey>, IEntitySet<TKey>, IEntitySetWithCreate<TKey>, IEntitySetWithCreateAndUpdate<TKey> where TKey : IConvertible
{
    /// <summary>
    /// 客户编号
    /// </summary>
    [Column("customer_id"), DisplayName("客户编号"), Required, MaxLength(36), Excel]
    public virtual TKey CustomerId { get; set; } = default!;
}

/// <summary>
/// 销售订单明细
/// </summary>
[Table("sale_order_detail"), DisplayName("销售订单明细")]
public class SaleOrderDetail : OrderDetail, IEntitySet, IEntitySetWithCreate
{
    /// <summary>
    /// 货品包装编号
    /// </summary>
    [Column("product_package_id"), DisplayName("货品包装编号"), MaxLength(36), Excel]
    public virtual string? ProductPackageId { get; set; }
    /// <summary>
    /// 单品总箱数
    /// 公式: Cartons => ProductPackage is null ? 0 : (Count - 1) / ProductPackage.Count + 1;
    /// 使用单品总数量推导总箱数是准确值
    /// </summary>
    [Column("cartons", TypeName = "decimal(12,2)"), DisplayName("总箱数"), Excel]
    public virtual decimal? Cartons { get; set; }

    /// <summary>
    /// 销售订单
    /// </summary>
    public virtual SaleOrder? Order { get; set; }
    /// <summary>
    /// 货品包装
    /// </summary>
    public virtual ProductPackage? ProductPackage { get; set; }
}

/// <summary>
/// 销售订单货运单
/// </summary>
[Table("Sale_Order_Transport"), DisplayName("销售订单货运单")]
public class SaleOrderTransport : OrderTransport, IEntitySet, IEntitySetWithCreate
{
    /// <summary>
    /// 销售订单
    /// </summary>
    public virtual SaleOrder? Order { get; set; }
}

/// <summary>
/// 销售订单支付记录
/// </summary>
[Table("sale_order_payment_record"), DisplayName("销售订单支付记录")]
public class SaleOrderPaymentRecord : AccountRecord<string>, IEntitySet, IEntitySetWithCreate;